
#define _CRT_SECURE_NO_DEPRECATE

#include "stdafx.h"
#include "Framework.h"
#include <commdlg.h>

#define DEFAULT_RDIALOG_WIDTH		(320)		// ray trace dialog
#define DEFAULT_RDIALOG_HEIGHT		(240)

#define BUTTON_MARGIN_V	(30)	// vertical margin for button
#define BUTTON_MARGIN_H	(20)	// horizontal margin for button
#define BUTTON_HEIGHT	(40)

// The panel window
HWND	PanelWnd;

// Control window elements : buttons
HWND	TraceButton;
HWND	LoadSceneButton;
HWND	SaveImageButton;

// Forwarded function decl
INT_PTR CALLBACK	RayTraceDialogProc(HWND, UINT, WPARAM, LPARAM);



bool CP_InitializeGUI(HWND hWnd)
{
	int top		= BUTTON_MARGIN_V;
	int width	= DEFAULT_CWINDOW_WIDTH - BUTTON_MARGIN_H * 2;

	TraceButton = CreateWindow(
		L"button",
		L"ray trace",
		WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
		BUTTON_MARGIN_H,
		top,
		width,
		BUTTON_HEIGHT,
		hWnd,
		(HMENU)BUTTON_TRACE,
		hInst,
		NULL
		);

	if (TraceButton == NULL)
		return FALSE;

	top += BUTTON_HEIGHT;
	LoadSceneButton = CreateWindow(
		L"button",
		L"load...",
		WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
		BUTTON_MARGIN_H,
		top,
		width,
		BUTTON_HEIGHT,
		hWnd,
		(HMENU)BUTTON_LOADSCENE,
		hInst,
		NULL
		);

	if (LoadSceneButton == NULL)
		return FALSE;

	top += BUTTON_HEIGHT;
	SaveImageButton = CreateWindow(
		L"button",
		L"save images...",
		WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
		BUTTON_MARGIN_H,
		top,
		width,
		BUTTON_HEIGHT,
		hWnd,
		(HMENU)BUTTON_SAVEIMAGE,
		hInst,
		NULL
		);

	if (SaveImageButton == NULL)
		return FALSE;

	RayTraceDialog = CreateDialog( hInst, MAKEINTRESOURCE(IDD_RAYTRACE), hWnd, RayTraceDialogProc );

	return RayTraceDialog != NULL;
}

bool CP_OnTrace(HWND hWnd)
{
	RECT	rect;

	ShowWindow( RayTraceDialog, SW_SHOW );
	// Move the dialog so that it fits the lower-right corner of the main window
	GetWindowRect( hWnd, &rect );

	MoveWindow(RayTraceDialog, 
		rect.right - DEFAULT_RDIALOG_WIDTH, 
		rect.bottom - DEFAULT_RDIALOG_HEIGHT,
		DEFAULT_RDIALOG_WIDTH,
		DEFAULT_RDIALOG_HEIGHT, 
		TRUE);

	return TRUE;
}

bool CP_OnSaveImage(HWND hWnd)
{
	// Initial and target filename
	TCHAR filename[256] = {0};
	GetModuleFileName((HMODULE)hInst, filename, 256);
	TCHAR* escapechar = wcsrchr(filename, '\\');
	escapechar[1] = 0;
	wcscat(filename, L"Image\\image");

	// Let user choose which name template to use
	OPENFILENAME	ofn;
	ZeroMemory(&ofn, sizeof(OPENFILENAME));

	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner	= hWnd;
	ofn.lpstrFilter = L"Image name template\0*.*\0\0";
	ofn.nFilterIndex= 1;
	ofn.lpstrFile	= filename;
	ofn.nMaxFile	= 256;
	ofn.lpstrTitle	= L"Image name template";
	ofn.Flags		= OFN_EXPLORER | OFN_OVERWRITEPROMPT;

	if (GetSaveFileName(&ofn))
	{
		// TODO[WWL] : save d3d image and ray trace result
		MessageBox(hWnd, filename, L"", MB_OK);
		return TRUE;
	}
	else
		return FALSE;
}

bool CP_OnLoadScene(HWND hWnd)
{
	// Initial and target filename
	TCHAR filename[256] = {0};
	GetModuleFileName((HMODULE)hInst, filename, 256);
	TCHAR* escapechar = wcsrchr(filename, '\\');
	escapechar[1] = 0;
	wcscat(filename, L"Scene\\scene.ase");

	// Let user choose which name template to use
	OPENFILENAME	ofn;
	ZeroMemory(&ofn, sizeof(OPENFILENAME));

	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hwndOwner	= hWnd;
	ofn.lpstrFilter = L"Scene file\0*.ase\0\0";
	ofn.nFilterIndex= 1;
	ofn.lpstrFile	= filename;
	ofn.nMaxFile	= 256;
	ofn.lpstrTitle	= L"Load scene...";
	ofn.Flags		= OFN_EXPLORER | OFN_FILEMUSTEXIST;

	if (GetOpenFileName(&ofn))
	{
		// TODO[WWL] : open ase file
		MessageBox(hWnd, filename, L"", MB_OK);
		return TRUE;
	}
	else
		return FALSE;
}

// Process messages for the control window
LRESULT CALLBACK ControlWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int		wmId, wmEvent;
	RECT	rect;
	PAINTSTRUCT ps;
	HDC		hDC;

	switch (message)
	{
	case WM_CREATE :
		if (!CP_InitializeGUI(hWnd))
			ErrorMessageDialog( GetLastError() );
		break;

	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);

		if (wmEvent == BN_CLICKED)
		{
			switch (wmId)
			{
			case BUTTON_TRACE :				
				if (!CP_OnTrace(hWnd))
					ErrorMessageDialog( GetLastError() );
				break;
			case BUTTON_LOADSCENE :
				if (!CP_OnLoadScene(hWnd))
					ErrorMessageDialog( GetLastError() );
				break;
			case BUTTON_SAVEIMAGE :
				if (!CP_OnSaveImage(hWnd))
					ErrorMessageDialog( GetLastError() );
				break;
			default :
				break;
			}
		}

		break;

	case WM_PAINT:
		hDC = BeginPaint(PanelWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(PanelWnd, &ps);
		break;

	case WM_ERASEBKGND : 
		hDC = reinterpret_cast<HDC>(wParam);
		GetClientRect(PanelWnd, &rect);
		rect.left -= 2;
		FillRect(hDC, &rect, CreateSolidBrush(RGB(43, 145, 175)));
		return 1;

	default :
		return DefWindowProc(hWnd, message, wParam, lParam);
	}

	return 0;	
}
